package org.sems.sems.Controller;

import org.sems.sems.Mapper.CsxgjlkMapper;
import org.sems.sems.Mapper.SemsConfigMapper;
import org.sems.sems.entity.Csxgjlk;
import org.sems.sems.entity.SemsConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/config")
public class SemsConfigController {

    @Autowired
    private SemsConfigMapper semsConfigMapper;
    @Autowired
    private CsxgjlkMapper csxgjlkMapper;
    @GetMapping("/getConfigById")
    public SemsConfig getConfigById(Integer id) {
        return semsConfigMapper.SelectConfigById(id);
    }
    @GetMapping("/getAllConfig")
    public Map<String, Object> getAllConfig() {
        Map<String, Object> dataMap = new HashMap<>();
        try {
            dataMap.put("data", semsConfigMapper.SelectAllConfig());
            dataMap.put("result", true);
            dataMap.put("code", 200);
            dataMap.put("msg", "success");
        } catch (Exception e) {
            dataMap.put("result", false);
            dataMap.put("code", 500);
            dataMap.put("msg", "server error");
            e.printStackTrace();
        }
        return dataMap;
    }
    @PostMapping("/updateConfigByMybatisXml")
    public Map<String, Object> updateConfigByMybatisXml(SemsConfig semsConfig) {
        Map<String, Object> dataMap = new HashMap<>();
        try {
            if (semsConfigMapper.UpdateConfigByMybatisXml(semsConfig) > 0) {
                dataMap.put("result", true);
                dataMap.put("code", 200);
                dataMap.put("msg", "success");
            } else {
                dataMap.put("result", false);
                dataMap.put("code", 200);
                dataMap.put("msg", "database error");
            }
        } catch (Exception e) {
            dataMap.put("result", false);
            dataMap.put("code", 500);
            dataMap.put("msg", "server error");
            e.printStackTrace();
        }
        return dataMap;
    }
    @PostMapping("/updateConfig")
    public Map<String, Object> updateConfig(int csdm, String value) {
        Map<String, Object> dataMap = new HashMap<>();
        try {

            if (semsConfigMapper.UpdateConfig(csdm, value) > 0) {
                dataMap.put("result", true);
                dataMap.put("code", 200);
                dataMap.put("msg", "success");
            } else {
                dataMap.put("result", false);
                dataMap.put("code", 200);
                dataMap.put("msg", "database error");
            }
        } catch (Exception e) {
            dataMap.put("result", false);
            dataMap.put("code", 500);
            dataMap.put("msg", "server error");
            e.printStackTrace();
        }
        return dataMap;
    }
    @GetMapping("/getAllCsxgjl")
    public Map<String, Object> getAllCsxgjl() {
        Map<String, Object> dataMap = new HashMap<>();
        try {
            dataMap.put("data", csxgjlkMapper.getCsxgjlk());
            dataMap.put("result", true);
            dataMap.put("code", 200);
            dataMap.put("msg", "success");
        } catch (Exception e) {
            dataMap.put("result", false);
            dataMap.put("code", 500);
            dataMap.put("msg", "server error");
            e.printStackTrace();
        }
        return dataMap;
    }

    // 批量获取数据 @RequestBody 用于接收 JSON 格式的 body，前端必须用 application/json 请求头。
    @PostMapping("/getSemsConfigByIds")
    public List<SemsConfig> getDataByIds(@RequestBody List<Integer> ids) {
        return semsConfigMapper.selectByIds(ids);
    }
}
